//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer;
using CommunityServer.Components;

namespace CommunityServer.Controls
{
	/// <summary>
	/// Summary description for AcceptInvitation.
	/// </summary>
	public class AcceptInvitation : SecureTemplatedWebControl
	{
		#region Child Controls

		IText AnonymousUserMessage;

		CSContext csContext = CSContext.Current;
		UserInvitation invitation;
		User user;

		#endregion

		public AcceptInvitation() : base()
		{
			// do we have an invitation key?
			if (Globals.IsNullorEmpty(csContext.Context.Request["InvitationKey"]))
				throw new CSException(CSExceptionType.UserAccountRequiresValidInvitation);

			// is the key a valid GUID?
			Guid invitationKey = Guid.Empty;
			try
			{
				invitationKey = new Guid(csContext.Context.Request["InvitationKey"]);
			}
			catch
			{
			}

			if (invitationKey == Guid.Empty)
				throw new CSException(CSExceptionType.UserAccountRequiresValidInvitation);

			// is the key a valid invitation?
			invitation = UserInvitations.GetUserInvitation(invitationKey);
			if (invitation == null)
				throw new CSException(CSExceptionType.UserAccountRequiresValidInvitation);

			user = Users.GetUser(invitation.UserID, false);
			if (user == null)
				throw new CSException(CSExceptionType.UserAccountRequiresValidInvitation);
		}

		protected override void OnInit(EventArgs e) 
		{
			if (SkinName == null)                
				ExternalSkinFileName = "Skin-AcceptInvitation.ascx";
			else 
				ExternalSkinFileName = SkinName;
			
			base.OnInit(e);
		}
		
		protected override void OnLoad(EventArgs e) 
		{
			base.OnLoad(e);

			UsersOnline.SetLocation("Accepting Invitation",null);

			DataBind();
		}

		public override void DataBind()
		{
			base.DataBind ();

			if (csContext.User.IsAnonymous)
			{
				if (AnonymousUserMessage != null)
				{
					AnonymousUserMessage.Visible = true;
					AnonymousUserMessage.Text = string.Format(ResourceManager.GetString("AcceptInvitation_AnonymousUser"), Page.Server.HtmlEncode(user.Username), Page.Server.HtmlEncode(invitation.Message), SiteUrls.Instance().UserRegisterWithInvitation(invitation.InvitationKey), SiteUrls.Instance().LoginWithInvitation(invitation.InvitationKey));
				}
			}
			else
			{
				UserInvitations.Accept(invitation, csContext.User);
				LeaveSecureConnection( Globals.GetSiteUrls().Message(CSExceptionType.UserInvitationAccepted));
			}
		}


		#region Skin
                
		protected override void AttachChildControls() 
		{
			AnonymousUserMessage = FindText("AnonymousUserMessage");
		}

		#endregion
	}
}
